package org.pierre.inputbenchmark.activity;

import org.pierre.inputbenchmark.R;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AccelerometerBenchmarkActivity extends FrequencyBenchmarkActivity implements SensorEventListener
{
	private TextView resultText;
	private SensorManager sm;
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.accelerometer);
		
		this.resultText = (TextView) this.findViewById(R.id.accelerometer_frequency);
		
		this.sm = (SensorManager) this.getSystemService(SENSOR_SERVICE);
		Sensor s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		this.sm.registerListener(this, s, SensorManager.SENSOR_DELAY_FASTEST);
	}
	
	protected void onDestroy()
	{
		super.onDestroy();
		
		this.sm.unregisterListener(this);
	}
	
	public void onAccuracyChanged(Sensor sensor, int accuracy)
	{
		// TODO Auto-generated method stub
		
	}
	
	public void onSensorChanged(SensorEvent event)
	{
		int averageFrequency = this.getAverageFrequency(event.timestamp / 1000000);
		
		this.resultText.setText(averageFrequency + " Hz");
	}
}
